package main.loading;

import gameStates.gui.LoadingState;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import com.jme.util.GameTaskQueueManager;

public abstract class LoadingProcess {
	
	protected LoadingState loadingState;
	protected List<LoadingStep> steps;
	protected boolean abort;
	
	public LoadingProcess(LoadingState loadingState) {
		this.loadingState = loadingState;
		
		steps = new ArrayList<LoadingStep>();
		
		initLoadingSteps();
	}
	
	public abstract boolean canBeAborted();
	
	public abstract void abort();
	
	public abstract void finish();
	
	public void cleanup() {
		GameTaskQueueManager.getManager().update(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				steps.clear();
				loadingState = null;
				return null;
			}
		});
	}
	
	protected abstract void initLoadingSteps();
	
	public LoadingState getLoadingState() { return loadingState; }
	
	public boolean hasNext(int i) { return i < steps.size(); }
	
	public LoadingStep getNextStep(int i) throws Exception { return steps.get(i); }
	
	protected void addLoadingStep(LoadingStep step) { steps.add(step); }
	
	protected void updateScreen(final int loadAmount) {
		GameTaskQueueManager.getManager().update(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				loadingState.updateLoadingScreen(loadAmount);
				return null;
			}
		});
	}
}